package pers.zxk.jlytnews.servlet;

import pers.zxk.jlytnews.dao.FeedbackDao;
import pers.zxk.jlytnews.dao.impl.FeedbackDaoImpl;
import pers.zxk.jlytnews.util.CommonUtil;
import pers.zxk.jlytnews.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.Date;


@MultipartConfig
public class LogServlet extends HttpServlet {
    private FeedbackDao mFeedbackDao = new FeedbackDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取能够与“url-pattern”中匹配的路径
        String method = req.getServletPath();

        //(此时处理的请求是查询 query.do)
        System.out.println("request.getServletPath()获取的值为: " + method);//输出 /query.do

        //2.通过字符串截取，把方法名 query 截取出来
        int i = method.lastIndexOf("/");
        method = method.substring(i + 1, method.length());

        System.out.println("截取后的值为： " + method);

        Method m = null;
        try {
            //3.获取当前类中名字为 method 的方法
            m = this.getClass().getDeclaredMethod(method,
                    HttpServletRequest.class, HttpServletResponse.class);

            //4.调用 method 方法
            m.setAccessible(true);
            m.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addFeedback(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        //获取Request传递（POST）过来的数据
        String title = req.getParameter("title");
        String contents = req.getParameter("contents");
        String contact = req.getParameter("contact");
        //String date = req.getParameter("date");//客户端获取的时间是不准确的，应该用服务器的时间
        String date = DateUtil.dateFormatFull(new Date());
        //更新数据并向客户端返回消息
        try {
            if (contents == null) {
                CommonUtil.sendJsonData(resp, "添加失败", 1, "反馈内容不能为空！");
            } else if (mFeedbackDao.addFeedback(title, contents, contact, date)) {
                CommonUtil.sendJsonData(resp, "添加成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "添加失败", 1, "");
            }
        } catch (Exception exception) {
            CommonUtil.sendJsonData(resp, "添加失败", 1, exception.getMessage());
        }
    }

    public void delFeedback(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        //获取Request传递（POST）过来的数据
        String id = req.getParameter("id");

        //更新数据并向客户端返回消息
        try {
            if (mFeedbackDao.delFeedback(Integer.parseInt(id))) {
                CommonUtil.sendJsonData(resp, "删除成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "删除失败", 1, "");
            }
        } catch (Exception exception) {
            CommonUtil.sendJsonData(resp, "删除失败", 1, exception.getMessage());
        }
    }

}
